use scopeguard::defer;

use bc_base::{TransactionId, constants::COINBASE_TX_ID_OF_GENESIS_BLOCK};
use bc_manager::LocalTransaction;

use tests::common;

#[tokio::test]
async fn query_tx() {
    let (test_env, tmp_dir) = common::setup("query_tx", false, None, false).await;
    defer! {
        tmp_dir.close().unwrap();
    };

    // do test here
    {
        let tx_id = TransactionId::from_hex_str(COINBASE_TX_ID_OF_GENESIS_BLOCK).unwrap();
        let bc_manager = test_env.bc_manager.lock().await;
        let tx = bc_manager.get_transaction(&tx_id).await.unwrap();
        let tx = match tx {
            LocalTransaction::TransactionInDb(tx) => tx,
            _ => panic!("tx is not in DB"),
        };
        assert_eq!(&tx.calc_tx_id(), &tx_id);
        assert_eq!(bc_manager.read_block_height_of_tx(&tx_id).await.unwrap(), 0);
    }

    common::teardown(test_env).await;
}
